/*********************************************************/
/*                                                       */
/* warlegue                                              */
/*                                                       */
/* button.h                                              */
/*                                                       */
/*********************************************************/

/*********************************************************/
/* includes                                              */
/*********************************************************/
#include "button.h"
#include <iostream>

CButton::CButton ()
{
}

CButton::CButton (float x, float y, const char* image, const char* imageover)
{
        /* load button image and make sprite */
        m_buttonTexture.LoadFromFile (image);
        m_buttonSprite.SetTexture (m_buttonTexture);
        m_buttonSprite.SetPosition (x, y);

        /* load the over button for later times */
        m_buttonOverTexture.LoadFromFile (imageover);
        m_over = false;
}

int CButton::Init (float x, float y, const char* image, const char* imageover)
{
        /* load button image and make sprite */
        m_buttonTexture.LoadFromFile (image);
        m_buttonSprite.SetTexture (m_buttonTexture);
        m_buttonSprite.SetPosition (x, y);

        /* load the over button for later times */
        m_buttonOverTexture.LoadFromFile (imageover);
        m_over = false;

        return 0;
}

void CButton::CenterWidth (float x)
{
        m_buttonSprite.SetPosition (x/2 - m_buttonSprite.GetTexture()->GetWidth()/2, m_buttonSprite.GetPosition().y);
}

bool CButton::Checkcollision (float x, float y)
{
        float left1, right1, top1, bottom1;

        left1 = m_buttonSprite.GetPosition ().x;
        right1 = left1 + m_buttonSprite.GetTexture()->GetWidth();
        top1 = m_buttonSprite.GetPosition ().y;
        bottom1 = top1 + m_buttonSprite.GetTexture()->GetHeight();

        if (left1 > x)
        {
                m_over = false;
                return false;
        }

        if (right1 < x)
        {
                m_over = false;
                return false;
        }

        if (top1 > y)
        {
                m_over = false;
                return false;
        }

        if (bottom1 < y)
        {
                m_over = false;
                return false;
        }

        m_over = true;
        return true;
}

sf::Sprite CButton::GetSprite ()
{
        if (m_over)
                m_buttonSprite.SetTexture (m_buttonOverTexture);
        else
                m_buttonSprite.SetTexture (m_buttonTexture);

        return m_buttonSprite;
}
void CButton::SetAlpha (int x)
{
        m_buttonSprite.SetColor (sf::Color (255, 255, 255, x));
}
